<?php
namespace app\admin\controller;

class Content extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
        $this->Content = D("Content");
    }

    public function listindex()
    {
        $data=[];
        if(IS_POST){
            $name = $_POST['name'];
            $data['name'] = array('like',"%$name%");
        }

        $select = M('content')->where($data)->order('id desc')->select();
        $count= count($select);
        $Page = $this->getPage($count,C('PAGE_NUMS'));
        $Content=M('content')->where($data)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->view->assign('Content',$Content);

        $this->view->parentterm = D('Term')->where(['parent_id'=>0])->select();

        return $this->view->fetch();
    }
    public function getlistpage($fetch = false,$term_id = null)
    {
        $data=[];
        if(isset($_POST['name'])){
            $name = $_POST['name'];
            $data['name'] = array('like',"%$name%");
        }

        if(isset($_POST['term_id'])){
            $term_id = $_POST['term_id'];
            if(empty(D('Term')->getChilds($term_id))){
                $data['term_id'] = array('eq',$term_id);
            }else{
                $data['term_id'] = array('in',D('Term')->getChilds($term_id));
            }

        }

        $modelId = D('term')->getTermInfo($term_id)['model_id'];
        $model = D('termModel')->getIdModelInfo($modelId);
        $this->view->assign('model',$model);


        $select = D('content')->where($data)->order('id desc')->select();
        $count= count($select);
        $Page = $this->getPage($count,50);
        $Content=D('content')->where($data)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->view->assign('Content',$Content);

        $this->view->parentterm = D('Term')->where(['parent_id'=>0])->select();

        $content = $this->view->fetch();
        if($fetch){
            return  $content;
        }else {
            echo $content;

        }

    }



    public function gettermchild($fetch = false,$term_id = null)
    {
        $term_id   = is_null($term_id) ? I('post.parent_id',"0","int") : $term_id;
        //$this->view->depth = $parent_id == 0 ?  array() : $this->Term->depth($parent_id);
        $this->view->parentterm = D('Term')->getList(['parent_id'=>$term_id]);

        $content = $this->view->fetch();

        if($fetch){
            return  $content;
        }else {
            if($this->view->parentterm){
                echo $content;
            }else{
                echo 2;
            }

        }
    }




    public function index($term_id){

        $select = M('content')->where('term_id='.$term_id)->select();
        $count= count($select);
        $Page = $this->getPage($count,C('PAGE_NUMS'));
        $Content=M('content')->where('term_id='.$term_id)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->view->assign('Content',$Content);
        $modelId = D('term')->getTermInfo($term_id)['model_id'];
        $model = D('termModel')->getIdModelInfo($modelId);
        $this->view->assign('model',$model);
        return $this->view->fetch();
    }

    public function addContent($term_id,$model_id){
        if(IS_POST){
            return $this->postAddContent($term_id,$model_id);
        }
        $termModelModel = D('TermModel');
        $formList = [];
        $termInfo = D('Term')->getTermInfo($term_id);
        $tagList = $termModelModel->getIdModelTags($termInfo['model_id'], 'content');
        $termModel = $termModelModel->getIdModelInfo($termInfo['model_id']);
        if($tagList){
            $formCon = A('Form');
            $formList = $formCon->createForm($tagList, [], 'tag');
        }
        $this->view->assign('termModel',$termModel);
        $this->view->assign('formList',$formList);
        return $this->view->fetch();
    }
    
    private function postAddContent($term_id,$model_id){
        if($_POST['title'] ==''){
            return $this->error('*为必选项,请检查是否完整');
        }else{
            $tagsValue = A('Form')->getValue('tag');
            $add_Content = $this->Content->addContent($model_id, $tagsValue);
            if($add_Content){
                manage_log_success();
                return $this->success('发布成功', U('Content/index',array('term_id'=>$term_id,'model_id'=>$model_id)));
            }else{
                manage_log_error();
                return $this->error('发布失败', U('Content/index',array('term_id'=>$term_id,'model_id'=>$model_id)));
            }
        }
    }

    public function delContent($term_id,$id,$model_id){
        $reconfirm = $this->reconfirm('删除后无法恢复，是否确认删除？');
        if(!$reconfirm){
            return $this->error('取消删除', U('Content/index',array('term_id'=>$term_id,'model_id'=>$model_id)));
        }
        $del_Content = $this->Content->delContent($id);
        if($del_Content){
            manage_log_success();
            return $this->success('删除成功', U('Content/index',array('term_id'=>$term_id,'model_id'=>$model_id)));
        }else{
            manage_log_error();
            return $this->error('删除失败', U('Content/index',array('term_id'=>$term_id,'model_id'=>$model_id)));
        }
    }

    public function editContent(){

        //栏目选择
        $all_term =D('Term')->selectAll();
        $fen_ji = $this->getTermTree(0,$all_term);
        $this->view->assign("all_term",$fen_ji);

        if(IS_POST)
            return $this->postEdit();
        $contentInfo = $this->Content->getInfo();
        $this->view->assign('edit_info',$contentInfo);
        if(isset($_SERVER['HTTP_REFERER']))
            cookie('editContent'.$contentInfo['id'].'from_url', $_SERVER['HTTP_REFERER']);

        $termModelModel = D('TermModel');
        //获取自定义标签
        $formList = [];
        $termInfo = D('Term')->getTermInfo($contentInfo['term_id']);
        $tagList = $termModelModel->getIdModelTags($termInfo['model_id'], 'content');
        $termModel = $termModelModel->getIdModelInfo($termInfo['model_id']);
        if($tagList){
            $formCon = A('Form');
            $formList = $formCon->createForm($tagList, json_decode($contentInfo['tags_value'],1), 'tag');
        }
        $this->view->assign('termModel',$termModel);
        $this->view->assign('formList',$formList);

        return $this->view->fetch();

    }

    public function postEdit(){
        $id         =   I('get.id');
        $term_id    =   I('get.term_id');
        $model_id   =   I('?get.model_id')?I('get.model_id'):0;
        $url = cookie('editContent'.$id.'from_url');
        if(empty($url))$url = U('Content/index',array('term_id'=>$term_id,'model_id'=>$model_id));
        $tagsValue = A('Form')->getValue('tag');
        $post_edit = $this->Content->postEdit($tagsValue);
        if($post_edit !=0){
            manage_log_success();
            return $this->success('编辑成功', $url);
        }else{
            manage_log_error();
            return $this->error('编辑失败', $url);
        }
        return false;
    }

    public function del_id($del_id)
    {

        $delet_id = $this->Content->delId($del_id);
        if($delet_id){
            manage_log_success();
        }else{
            manage_log_error();
        }
    }

    public function listorder($listorder,$listorder_id)
    {
        $list_order = $this->Content->listOrder($listorder,$listorder_id);
        if($list_order){
            manage_log_success();
            return true;
        }else{
            manage_log_error();

        }
    }

    //搜索分页显示
    public function search($term_id=null,$model_id=null,$name=null)
    {
        $data['term_id'] = $term_id;
        $data['model_id'] = $model_id;
        $data['name'] = array('like',"%$name%");
        $search_info = M('Content')->where($data)->select();
        $count= count($search_info);
        $Page = $this->getPage($count,C('PAGE_NUMS'));
        $search = M('Content')->where($data)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->view->assign('search',$search);
        return $this->view->fetch();
    }

    //保留原有发布内容的形式，增加侧边栏发布内容功能

    public function addSliderContent()
    {

        if(IS_POST){
            if($_POST['term_id'] !=""){
                if($_POST['title'] !="" )
                {
                    $add_slider = $this->Content->addSliderContent();
                    if($add_slider){
                        manage_log_success();
                        return $this->success('发布成功');
                    }else{
                        manage_log_error();
                        return $this->error('发布失败');
                    }
                }

            }else{
                manage_log_error();
                return $this->error('*为必选项,请检查是否完整');	
            }


        }

        //所属栏目
        $parent =D('Term')->selectAll();
        $parent = $this->getTermTree(0,$parent);
        $this->view->assign('parent',$parent);
        return $this->view->fetch();
    }

    //暂时这么处理，再优化
    public function ajaxSelectTwo()
    {
        $Two = $_POST['Two'];
        $parent_one = M('term')->where('parent_id='.$Two)->select();
        echo json_encode($parent_one);
    }
    public function ajaxSelectThree()
    {
        $Three = $_POST['Three'];
        $parent_one = M('term')->where('parent_id='.$Three)->select();
        echo json_encode($parent_one);

    }
    public function ajaxSelectFour()
    {
        $Four = $_POST['Four'];
        $parent_one = M('term')->where('parent_id='.$Four)->select();
        echo json_encode($parent_one);
    }
    //暂时这么处理，再优化 END
































}
